<?php


namespace App\Remote\Feishu\Sso;

use App\Exceptions\BaseException;
use App\Kuafu\User\Enum\UserCodeEnum;
use Closure;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\HttpFoundation\Request as RequestAlias;


class SsoRemote extends SsoBaseRemote
{

    /**
     * @param int $type
     * @return array
     * @throws Exception
     */
    public static function getOauthTypeConfig(int $type): array
    {
        switch ($type) {
            case UserCodeEnum::MERCHANT_TYPE:
                $config = config("fsOauthClient.merchant");
                break;
            default:
                $config = [];
                break;
        }
        if (empty($config)) {
            throw new Exception("飞书第三方授权登陆配置缺失 type : {$type}");
        }

        return [$config["clientId"], $config["clientSecret"]];
    }

    public static array $accessToken = [];

    /**
     * 获取飞书第三方授权登陆token
     *
     * @param int $type
     * @param string $code
     * @return mixed|void
     * @throws BaseException
     * @throws GuzzleException
     */
    public static function getAccessToken(int $type, string $code)
    {
        list($clientId, $clientSecret) = self::getOauthTypeConfig($type);
        if (empty(self::$accessToken)) {
            self::$accessToken = self::customRequest(
                "/suite/passport/oauth/token",
                RequestAlias::METHOD_POST,
                [
                    "grant_type"    => "authorization_code",
                    "client_id"     => $clientId,
                    "client_secret" => $clientSecret,
                    "code"          => $code,
                ],
                ["Content-Type" => "application/x-www-form-urlencoded"]
            );
        }

        return self::$accessToken;
    }


    /**
     * 飞书第三方授权登陆token刷新
     *
     * @param string $refreshToken
     * @return mixed|void
     * @throws BaseException
     * @throws GuzzleException
     */
    public static function refreshAccessToken(string $refreshToken)
    {
        return self::customRequest(
            "/suite/passport/oauth/token",
            RequestAlias::METHOD_POST,
            [
                "grant_type"    => "refresh_token",
                "refresh_token" => $refreshToken
            ],
            ["Content-Type" => "application/x-www-form-urlencoded"]
        );
    }

    public static function registerResponseHandle(): Closure
    {
        return parent::registerResponseHandle(); // TODO: Change the autogenerated stub
    }

    /**
     * 飞书第三方登陆获取用户信息
     *
     * @param int $type
     * @param string $code
     * @return mixed|void
     * @throws BaseException
     * @throws GuzzleException
     */
    public static function userinfo(int $type, string $code)
    {
        return self::customRequest(
            "/suite/passport/oauth/userinfo",
            RequestAlias::METHOD_GET,
            [],
            [
                "Authorization" => sprintf(
                    "Bearer %s",
                    self::getAccessToken($type, $code)["access_token"]
                )
            ]
        );
    }


}
